package org.bookie.facebook.components;

import static org.bookie.core.services.impl.Listener.Event.BET_SLIP_PLACED;
import static org.bookie.core.services.impl.Listener.Event.USER_LOGGED_IN;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.bookie.core.services.impl.Listener;
import org.bookie.facebook.pages.Home;
import org.bookie.framework.CommonBaseComponent;
import org.bookie.framework.components.BetSlipComponent;
import org.bookie.model.BetSlip;
import org.bookie.model.services.EventDAO;
import org.bookie.model.services.UserDAO;

public abstract class Border extends CommonBaseComponent {
	
	/**Client identifier of the page header*/
	public static final String HEADER = "header";
	/**Client identifier of the border errors*/
	public static final String BORDER_ERRORS = "borderErrors";
	
	public static final Map<String, String> pages = new HashMap<String, String>();
	
	static {
		pages.put("Home", "Main Page");
		pages.put("Results", "Results");
		pages.put("Friends", "My Friends Bets");
	}
	
	/**
	 * Get the bet slip of the user.
	 * 
	 * @return instance of BetSlip
	 */
	public abstract BetSlip getBetSlip();

	/**
	 * Injected event service.
	 * 
	 * @return instance of EventDAO
	 */
	@InjectObject("service:bookie.model.EventDAO")
	public abstract EventDAO getEventDAO();
	
	@InjectObject("service:bookie.model.UserDAO")
	public abstract UserDAO getUserDAO();

	/**
	 * Injected home page.
	 * 
	 * @return instance of Home
	 */
	@InjectPage("Home")
	public abstract Home getHome();

	/**
	 * Injected service to restart the session.
	 * 
	 * @return instance of IEngineService
	 */
	@InjectObject("engine-service:restart")
	public abstract IEngineService getRestartService();

	/**
	 * Listener method to log out the user.
	 * 
	 * @return link to the Tapestry restart service
	 */
	public ILink logout() {
		getAuthenticationContext().logout();
		return getRestartService().getLink(false, null);
	}
	
	public void refill(){
		boolean result = getAuthenticationContext().getUser().getAccount().refill();
		if(!result){
			recordError("border.refill.message");
			updateComponents(BORDER_ERRORS);
		}
		updateComponents(Border.HEADER);
	}

	/**
	 * Listener method that is triggered when user logged
	 * in by the Login component
	 * @see ILoginComponentListener
	 */
	@Listener(USER_LOGGED_IN)
	public void userLoggedIn(String clientId) {
		getResponseBuilder().updateComponent(Border.HEADER);
	}
	
	/**
	 * Listener method that is triggered when user placed bet slip
	 * @see BetSlipComponent
	 */
	@Listener(BET_SLIP_PLACED)
	public void userPlacedBetSlip() {
		getUserDAO().refresh(getAuthenticationContext().getUser());
		getResponseBuilder().updateComponent(Border.HEADER);
	}
	
	/**
	 * Returns pages
	 * 
	 * @return
	 */
	public Set<Entry<String, String>> getPages() {
		return pages.entrySet();
	}
	
	/**
	 * Returns true in case given page which is rendered is the same as page inside {@link #getApage()}
	 * 
	 * @return
	 */
	public boolean isPageSelected() {
		return getPage().getPageName().equals(getApage().getKey());
	}
	
	public abstract Entry<String, String> getApage();
}
